package android.gpsmessenger;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class ContactProfileFragment extends Fragment
{	
	private ContactList contacts = new ContactList();
	private ContactManager contactManager;
	private String name, phone, email, id;
	private Contact contact;
	private boolean saveData = true;
	
	private EditText name_field;
	private EditText phone_field ;
	private EditText email_field ;
	
	public void onCreate (Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
		contactManager = new ContactManager(getActivity());
		name = getActivity().getIntent().getStringExtra("name");
		phone = getActivity().getIntent().getStringExtra("phone");
		email = getActivity().getIntent().getStringExtra("email");
		id = getActivity().getIntent().getStringExtra("id");
		contact = new Contact(name, phone, email, id);
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{
		return inflater.inflate(R.layout.fragment_contact_profile, container, false);
	}

	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
	{
		inflater.inflate(R.menu.fragment_ok_cancel_menu, menu);
	}
	
	public void onResume() 
	{
		super.onResume();
		saveData = true;

		contacts = contactManager.load();
		if (name != null) 
		{
			Contact data = contacts.getContact(contact);

			name_field  = (EditText) getActivity().findViewById(R.id.name);
			phone_field = (EditText) getActivity().findViewById(R.id.phone);
			email_field = (EditText) getActivity().findViewById(R.id.email);

			name_field.setText(data.getName());
			phone_field.setText(data.getPhone());
			email_field.setText(data.getEmail());
		}
	}

	public void onPause() 
	{
		super.onPause();
		if (saveData) 
		{
			Contact data = null;
			
			name_field  = (EditText) getActivity().findViewById(R.id.name);
			phone_field = (EditText) getActivity().findViewById(R.id.phone);
			email_field = (EditText) getActivity().findViewById(R.id.email);   
	    
			String inputName = name_field.getText().toString();
			String inputPhone = phone_field.getText().toString();
			String inputEmail = email_field.getText().toString();
			id = getActivity().getIntent().getStringExtra("id");

	           
			if (name != null) 
			{
				data = contacts.getContact(contact);
				contacts.remove(data);
			}
				
			data = new Contact(inputName, inputPhone, inputEmail, id);
			contacts.add(data);
			contactManager.save(contacts);
		}
	}
	
	public boolean onOptionsItemSelected (MenuItem item) 
	{
		switch (item.getItemId()) 
		{
		 	case R.id.menu_ok:
		 		if(validated()) 
		 		{
		 			saveData = true;
		 			getActivity().finish();
		 		} 
		 		else 
		 		{
		 			Toast.makeText(getActivity(), 
		 			"You have missing or incorrect values in some form fieds!", Toast.LENGTH_SHORT).show();
		 		}
		 		break;
		 	case R.id.menu_cancel:
		 		saveData = false;
		 		getActivity().finish();
		 		break;
		 }
		return true;
	}
		  
	protected boolean validated() 
	{		  
		boolean validated = true;
		
		name_field  = (EditText) getActivity().findViewById(R.id.name);
		phone_field = (EditText) getActivity().findViewById(R.id.phone);
		email_field = (EditText) getActivity().findViewById(R.id.email);   
		  
		if (!Validate.hasText(name_field)) validated = false;
		if (!Validate.hasText(phone_field)) validated = false;
		if (!Validate.hasText(email_field)) validated = false;
		
		if (!Validate.isEmailAddress(email_field, true)) validated = false;
		//if (!Validate.isPhoneNumber(phone_field, true)) validated = false;
		//if (!Validate.isEmailAddress(email_field.getText().toString())) validated = false;
		
		return validated;
	}	
}

/*
 	public boolean onOptionsItemSelected (MenuItem item) {
		if (item.getItemId() == R.id.menu_ok)
			saveData = true;
		else if (item.getItemId() == R.id.menu_cancel)
			saveData = false;
		getActivity().finish();
		return true; 
 */
